<template>
  <div>
    <el-dialog v-loading.lock="loading"
      class="base-dialog system-user-dialog"
      :append-to-body="true"
      title="物料出入库检验单产品信息"
      align="center"
      :close-on-click-modal="false"
      :visible.sync="innerDialogFormVisible">
      <el-form ref="formData"
        class="base-pipeline-layout system-user-form"
        label-width="100px"
        :model="formData"
        :rules="formDataRules"
        status-icon>
        <el-form-item label="订单id"
          :label-width="formLabelWidth"
          prop="orderId"
          :error="errorMsg.orderId">
          <el-input v-model="formData.orderId"
            autocomplete="off"
            type="number"></el-input>
        </el-form-item>
        <el-form-item label="产品Id"
          :label-width="formLabelWidth"
          prop="productId"
          :error="errorMsg.productId">
          <el-input v-model="formData.productId"
            autocomplete="off"
            type="number"></el-input>
        </el-form-item>
        <el-form-item label="产品编号"
          :label-width="formLabelWidth"
          prop="productNum"
          :error="errorMsg.productNum">
          <el-input v-model="formData.productNum"
            autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="应检数量"
          :label-width="formLabelWidth"
          prop="totalCount"
          :error="errorMsg.totalCount">
          <el-input v-model="formData.totalCount"
            autocomplete="off"
            type="number"></el-input>
        </el-form-item>
        <el-form-item label="待检数量"
          :label-width="formLabelWidth"
          prop="pendingCount"
          :error="errorMsg.pendingCount">
          <el-input v-model="formData.pendingCount"
            autocomplete="off"
            type="number"></el-input>
        </el-form-item>
        <el-form-item label="已检数量"
          :label-width="formLabelWidth"
          prop="inspectedCount"
          :error="errorMsg.inspectedCount">
          <el-input v-model="formData.inspectedCount"
            autocomplete="off"
            type="number"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer"
        class="dialog-footer"
        align="center">
        <el-button type="primary"
          @click="onSubmit('formData')">保 存</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { create, update } from "../../../api/Material/MaterialInspectionProduct.js";

export default {
  name: "MaterialInspectionProductEdit",
  props: {
    dialogFormVisible: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      loading: false,
      innerDialogFormVisible: false,
      formLabelWidth: "100px",
      initData: {
        id: 0,
        orderId: 0,
        productId: 0,
        productNum: "",
        totalCount: 0,
        pendingCount: 0,
        inspectedCount: 0,
      },
      formData: {
        id: 0,
        orderId: 0,
        productId: 0,
        productNum: "",
        totalCount: 0,
        pendingCount: 0,
        inspectedCount: 0,
      },
      formDataRules: {
        orderId: [
          {
            required: true,
            trigger: "blur",
            validator: this.Helper.validateFunctionPositiveNumber,
          },
        ],
        productId: [
          {
            required: true,
            trigger: "blur",
            validator: this.Helper.validateFunctionPositiveNumber,
          },
        ],
        productNum: [
          {
            required: true,
            trigger: "blur",
            validator: this.Helper.validateFunction,
          },
        ],
        totalCount: [
          {
            required: true,
            trigger: "blur",
            validator: this.Helper.validateFunctionPositiveNumber,
          },
        ],
        pendingCount: [
          {
            required: true,
            trigger: "blur",
            validator: this.Helper.validateFunctionPositiveNumber,
          },
        ],
        inspectedCount: [
          {
            required: true,
            trigger: "blur",
            validator: this.Helper.validateFunctionPositiveNumber,
          },
        ],
      },
      initErrorMsg: {
        orderId: "",
        productId: "",
        productNum: "",
        totalCount: "",
        pendingCount: "",
        inspectedCount: "",
      },
      errorMsg: {
        orderId: "",
        productId: "",
        productNum: "",
        totalCount: "",
        pendingCount: "",
        inspectedCount: "",
      },
    };
  },
  watch: {
    dialogFormVisible(val) {
      this.innerDialogFormVisible = val;
    },
    innerDialogFormVisible(val) {
      this.$emit("update:dialogFormVisible", val);
    },
  },
  methods: {
    cancel() {
      this.innerDialogFormVisible = false;
    },
    closeDialog() {
      this.$parent.getList();
      this.cancel();
    },
    onSubmit() {
      this.$refs.formData.validate((valid) => {
        if (valid) {
          this.loading = true;
          this.errorMsg = this.Helper.deepCopy(this.initErrorMsg);
          if (this.formData.id <= 0) {
            create(this.formData)
              .then((res) => {
                if (res.code === 200) {
                  this.closeDialog();
                } else {
                  this.showValidateMsgCheck(res);
                }
                this.loading = false;
              })
              .catch(() => {
                this.loading = false;
              });
          } else {
            update(this.formData)
              .then((res) => {
                if (res.code === 200) {
                  this.closeDialog();
                } else {
                  this.showValidateMsgCheck(res);
                }
                this.loading = false;
              })
              .catch(() => {
                this.loading = false;
              });
          }
        } else {
          return false;
        }
      });
    },
    showValidateMsgCheck(res) {
      if (res.msg) {
        this.Helper.showValidateMsg("orderId", this.errorMsg, res.msg);
        this.Helper.showValidateMsg("productId", this.errorMsg, res.msg);
        this.Helper.showValidateMsg("productNum", this.errorMsg, res.msg);
        this.Helper.showValidateMsg("totalCount", this.errorMsg, res.msg);
        this.Helper.showValidateMsg("pendingCount", this.errorMsg, res.msg);
        this.Helper.showValidateMsg("inspectedCount", this.errorMsg, res.msg);
      }
    },
  },
};
</script>
